//
//  DeviceAndTollCell.m
//  Runone
//
//  Created by runone02 on 16/11/18.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "DeviceAndTollCell.h"
#import "DeviceBaseInfo.h"
#import "OneConst.h"
#import "LineMergeJsonModel.h"
#import "AppDelegate.h"
#import "HighWayRoadRecordDirectionInfoModel.h"
@interface DeviceAndTollCell ()

@property (weak, nonatomic) IBOutlet UIView *leftView;
@property (weak, nonatomic) IBOutlet UIButton *stationBtn;
@property (weak, nonatomic) IBOutlet UIView *rightView;

@property (weak, nonatomic) IBOutlet UIButton *leftBoardBtn;
@property (weak, nonatomic) IBOutlet UIButton *rightBoardBtn;
@property (weak, nonatomic) IBOutlet UIButton *leftCameraBtn;
@property (weak, nonatomic) IBOutlet UIButton *rightCameraBtn;
@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;

@property (nonatomic, strong) HighwayMergeRoadRecordModel *currentRoadModel;
@property (nonatomic, strong) HighwayMergeInfoModel       *mergeModel;

@property (nonatomic, strong) NSArray *upBoardArray;
@property (nonatomic, strong) NSArray *upCameraArray;
@property (nonatomic, strong) NSArray *downBoardArray;
@property (nonatomic, strong) NSArray *downCameraArray;

@end


@implementation DeviceAndTollCell
#pragma mark - 按钮标记
- (void) setBtnTag {
    _leftBtn.tag        = 0;
    _stationBtn.tag     = 1;
    _rightBtn.tag       = 2;
    _leftBoardBtn.tag   = 3;
    _leftCameraBtn.tag  = 4;
    _rightBoardBtn.tag  = 5;
    _rightCameraBtn.tag = 6;
}
#pragma mark -按钮监听
- (void) monitorBtnclick {
    [_leftBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_stationBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_rightBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_leftBoardBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_leftCameraBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_rightBoardBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_rightCameraBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)btnClick:(UIButton *)btn {
    
    switch (btn.tag) {
        case 0: {
            NSLog(@"左");
            LineMergeJsonModel *lineMerge = _mergeModel.mergeJsonArray[0];
            AppDelegate *myDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
            HighwayMergeRoadRecordModel *roadModel0 = myDelegate.HighWayRoadArray[0];
            HighwayMergeRoadRecordModel *roadModel1 = myDelegate.HighWayRoadArray[1];
            HighwayMergeRoadRecordModel *roadModel2 = myDelegate.HighWayRoadArray[2];
            if ([lineMerge.CrossRoadUID isEqualToString:roadModel0.RoadUID] ) {
                self.ClickButton(btn.tag, nil, roadModel0);
            }
            if ([lineMerge.CrossRoadUID isEqualToString:roadModel1.RoadUID] ) {
                self.ClickButton(btn.tag, nil, roadModel1);
            }
            if ([lineMerge.CrossRoadUID isEqualToString:roadModel2.RoadUID] ) {
                self.ClickButton(btn.tag, nil, roadModel2);
            }
        }
        break;
        case 1: {
            NSLog(@"%ld",(long)btn.tag);
            NSLog(@"收费站");
            self.ClickButton(btn.tag, nil, _currentRoadModel);
        }
            break;
        case 2: {
            LineMergeJsonModel *lineMerge = _mergeModel.mergeJsonArray[0];
            AppDelegate *myDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
            HighwayMergeRoadRecordModel *roadModel0 = myDelegate.HighWayRoadArray[0];
            HighwayMergeRoadRecordModel *roadModel1 = myDelegate.HighWayRoadArray[1];
            HighwayMergeRoadRecordModel *roadModel2 = myDelegate.HighWayRoadArray[2];
            if ([lineMerge.CrossRoadUID isEqualToString:roadModel0.RoadUID] ) {
                self.ClickButton(btn.tag, nil, roadModel0);
            }
            if ([lineMerge.CrossRoadUID isEqualToString:roadModel1.RoadUID] ) {
                self.ClickButton(btn.tag, nil, roadModel1);
            }
            if ([lineMerge.CrossRoadUID isEqualToString:roadModel2.RoadUID] ) {
                self.ClickButton(btn.tag, nil, roadModel2);
            }
        }
            break;
        case 3: {
            self.ClickButton(btn.tag, _upBoardArray, _currentRoadModel);
        }
            break;
        case 4: {
            self.ClickButton(btn.tag, _upCameraArray, _currentRoadModel);
        }
            break;
        case 5: {
            self.ClickButton(btn.tag, _downBoardArray, _currentRoadModel);
        }
            break;
        case 6: {
            self.ClickButton(btn.tag, _downCameraArray, _currentRoadModel);
        }
            break;
        default:
            break;
    }
}

#pragma mark - 初始
- (void)awakeFromNib {
    [super awakeFromNib];
    _currentRoadModel = [[HighwayMergeRoadRecordModel alloc]init]; //当前的模型
    [self monitorBtnclick];
    self.selectionStyle =  UITableViewCellSelectionStyleNone;
    [self setDefaultUIHidden];
    [self setBtnTag];
    
}

- (void)setDefaultUIHidden { //默认将收费站按钮之外的其他控件都隐藏了,同时让收费站按钮默认不能点
    _leftView.hidden                   = YES;
    _rightView.hidden                  = YES;
    _stationBtn.hidden                 = NO;
    _stationBtn.userInteractionEnabled = YES;
//    _midView.hidden                    = YES;
    _rightBoardBtn.hidden               = YES;
    _rightCameraBtn.hidden              = YES;
    _leftBoardBtn.hidden                = YES;
    _leftCameraBtn.hidden               = YES;
    _leftBtn.hidden                    = YES;
    _rightBtn.hidden                   = YES;
    _midView.hidden                    = NO;
}
//_DirectionDescription	__NSCFString *	@"往韶赣方向"	0x00000001748488e0
-(void)setHighWayRoadModel:(HighwayMergeRoadRecordModel *)roadModel andMergeInfo:(HighwayMergeInfoModel *)mergrModel {
    _currentRoadModel                                     = roadModel;
    _mergeModel                                           = mergrModel;
   
    _upBoardArray     = [self classfyDevicesData:_mergeModel.UpWayDevices andType:DevicesTypeBoard];
    _upCameraArray    = [self classfyDevicesData:_mergeModel.UpWayDevices andType:DevicesTypeCamera];
    _downBoardArray   = [self classfyDevicesData:_mergeModel.DownWayDevices andType:DevicesTypeBoard];
    _downCameraArray  = [self classfyDevicesData:_mergeModel.DownWayDevices andType:DevicesTypeCamera];
    [self judgeDeviceBtn];       //控制设备按钮
    [self judgeTollStationName]; //控制收费站按钮以及支线按钮
}

#pragma mark - 设备数据
- (NSArray *) classfyDevicesData:(NSArray *)array andType:(DevicesType)type  {
    NSMutableArray *tempArray = [[NSMutableArray alloc]init];
    for (DeviceBaseInfo *deviceInfo in array) {
        if (deviceInfo.DeviceType == type ) {
            [tempArray addObject:deviceInfo];
        }
    }
    return tempArray;
}
#pragma mark -控制设备按钮
- (void) judgeDeviceBtn {
    if (_upBoardArray.count > 0) {
        _leftBoardBtn.hidden = NO;
    }
    if (_upCameraArray.count > 0) {
        _leftCameraBtn.hidden = NO;
    }
    
    if (_downBoardArray.count > 0) {
        _rightBoardBtn.hidden = NO;
    }
    if (_downCameraArray.count > 0) {
        _rightCameraBtn.hidden = NO;
    }
}

#pragma mark - 收费站
- (void) judgeTollStationName {
    [_stationBtn setTitle:_mergeModel.mergeName forState:UIControlStateNormal];
    LineMergeJsonModel *lineModel = _mergeModel.mergeJsonArray[0];
    [_leftBtn setTitle:lineModel.CrossRoadCode forState:UIControlStateNormal];
    [_rightBtn setTitle:lineModel.CrossRoadCode forState:UIControlStateNormal];
    if (_mergeModel.mergeType == 1  ) {
        _stationBtn.userInteractionEnabled = YES;//让收费站可以点
    }
    if (_mergeModel.mergeType == 0) { //起始或者结束位置
        _stationBtn.userInteractionEnabled = NO;
    }
    if (_mergeModel.mergeType == 2) { //枢纽
        _stationBtn.userInteractionEnabled = NO;
        _leftView.hidden                   = NO;
        _rightView.hidden                  = NO;
        LineMergeJsonModel *lineModel = _mergeModel.mergeJsonArray[0];
        if (lineModel.IsTwoWay == 0) { //双边
            _leftBtn.hidden                  = NO;
            _rightBtn.hidden                 = NO;
        }
        if (lineModel.IsTwoWay == 1) { //左边
            _leftBtn.hidden                  = NO;
            _rightBtn.hidden                 = YES;
        }
        
        if (lineModel.IsTwoWay == 2) { //右边
            _rightBtn.hidden                  = NO;
            _leftBtn.hidden                   = YES;
        }
    }
}

- (void)prepareForReuse {
    [super prepareForReuse];
    [self setDefaultUIHidden];
}

@end
